package com.tanguy.driver.ui.account.activity

import android.app.Activity
import android.text.InputType
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import cn.sinata.xldutils_kotlin.rxutils.ResultException
import com.tanguy.driver.R
import com.tanguy.driver.base.ImmersionTitleActivity
import com.tanguy.driver.net.http.HttpManager
import com.tanguy.driver.utlis.extend.*
import com.tanguy.user.utlis.extend.click
import io.reactivex.Observable
import org.jetbrains.anko.find

/**
 * 忘记密码
 */

class ForgetPasswordActivity : ImmersionTitleActivity() {
    override fun setContentViewRes(): Int = R.layout.activity_forget_password
    private val mTvCaptcha by lazy { find<TextView>(R.id.tv_get_captcha) }
    private val mTvPlaintext by lazy { find<ImageView>(R.id.tv_plaintext) }
    private val mEtPassword by lazy { find<EditText>(R.id.et_password) }
    private var isPlaintext = false//是明文显示
    override fun initView() {
        super.initView()
        title = "忘记密码"
    }

    override fun initEvent() {
        super.initEvent()
        click(R.id.btn_next) {
            val phone = getEditTextString(R.id.et_phone)
            val captcha = getEditTextString(R.id.et_captcha)
            val password = getEditTextString(mEtPassword)
            if (!checkPhoneNumber(phone)) return@click
            if (!checkCaptcha(captcha)) return@click
            if (!checkPassword(password)) return@click
            updatePasw(phone,password,captcha)
        }
        click(R.id.tv_get_captcha) {
            val phone = getEditTextString(R.id.et_phone)
            if (!checkPhoneNumber(phone)) return@click
            HttpManager.sendSmsCode(phone, 5)
                    .requestSucceed(this) {
                        captchaCountDownTimer { finish, msg ->
                            mTvCaptcha.isClickable = finish
                            find<TextView>(R.id.tv_get_captcha).text = msg
                        }
                    }
        }
        click(R.id.tv_plaintext) {
            if (isPlaintext) {
                //密文
                mEtPassword.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
                mTvPlaintext.setBackgroundResource(R.mipmap.yanjing_2)
            } else {
                //明文
                mEtPassword.inputType = InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
                mTvPlaintext.setBackgroundResource(R.mipmap.yanjing_1)
            }
            isPlaintext = !isPlaintext
            mEtPassword.setSelection(mEtPassword.text.length)
        }
    }

    private fun updatePasw(phone: String, password: String, captcha: String) {
        HttpManager.yzCode(phone, 5, captcha)
                .flatMap {
                    if (it.code == 0) {
                        HttpManager.updatePasw(phone, password)
                    } else {
                        Observable.error(ResultException(it.code, it.msg))
                    }
                }.requestSucceed(this) {
            showToast("找回成功")
            setResult(Activity.RESULT_OK)
            finish()
        }

    }
}
